setwd("/Users/bsjjones/Dropbox/JoestenJones/PB R&R")

#This plots the unconditional and conditional models using the micro-level data. Relevant do file 
#is "cues_micromodels.do"  This plot corresponds to Figure 2 in the main body of the paper. 

library(foreign)
library(rmeta)
library(plotrix)


#########################
INDIVIDUAL-LEVEL MODELS
#########################




##########################
#Regression Plots for Micro Data
##########################
pdf("figure2_RplotALT.pdf", width=7, height=7)
par(mfrow=c(2,3),oma=c(0,0,2,0))
par(mar=c(3, 7, 1, 1))

r1<-read.dta("repvoterm1.dta")
attach(r1)
coef.vec<-beta
se.vec<-se
y.axis<-c(length(coef.vec):1)
var.names<-label

plot(coef.vec, y.axis, type = "p", axes = F, xlab = "Log-Odds Estimates", ylab = "", pch = 20, cex = .65, xlim = c(-2,5), xaxs = "r", main = "Republican", cex.main=.85, cex.lab=.75, mgp=c(2,.7,0)) 
#plot coefficients as points, turning off axes and labels. 

#set limits of x-axis so that they include mins and maxs of 
#coefficients + .95% confidence intervals and plot is symmetric; use "internal axes", and leave plot title empty
#the 3 lines below create horiztonal lines for 95% confidence intervals, and vertical ticks for 90% intervals

segments(coef.vec-qnorm(.975)*se.vec, y.axis, coef.vec+qnorm(.975)*se.vec, y.axis, lwd =  1, lend="butt")

#coef +/-1.96*se = 95% interval, lwd adjusts line thickness
#if you want to add tick marks for 90% confidence interval, use following 2 lines:
#segments(coef.vec-qnorm(.95)*se.vec, y.axis -.1, coef.vec-qnorm(.95)*se.vec, y.axis +.1, lwd = 1.1)#coef +/-1.64*se = 90% interval
#segments(coef.vec+qnorm(.95)*se.vec, y.axis -.1, coef.vec+qnorm(.95)*se.vec, y.axis +.1, lwd = 1.1)

axis(1, at = seq(-2,5,by=1), labels = NA, tick = T,#draw x-axis and labels with tick marks
    cex.axis = .65, mgp = c(2,.7,0))#reduce label size, moves labels closer to tick marks
axis(1, at = seq(-2,5,by=1), labels =  c(-2, -1, 0,  1, 2,  3, 4, 5), tick = T,#draw x-axis and labels with tick marks
    cex.axis = .65, mgp = c(2,.7,0))#reduce label size, moves labels closer to tick marks    

axis(2, at = y.axis, label = var.names,   las = 1, tick = F ,mgp = c(2,.6,0),
    cex.axis = .75) #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
segments(0,0,0,17,lty=2) # draw dotted line through 0
box(bty = "l") #place box around plot
x.height <- .20
#text(x.height, 1.75, expression(R^{2} == .13), adj = 0, cex = .75) #add text for R-squared
#text(x.height, 1.5, expression(beta[0] == 3.51~(.62)), adj = 0, cex = .75)#add text for Adjusted-R-squared
text(x.height, 1.25, "n = 771", adj = 0, cex = .75)#add text for sample size

rm(list=ls())
detach(r1)

####### Dem M1

rm(list=ls())


#par(mfrow=c(1,2),oma=c(0,0,2,0))
#par(mar=c(4, 7, 2, 2))

d1<-read.dta("demvoterm1.dta")
attach(d1)
coef.vec<-beta
se.vec<-se
y.axis<-c(length(coef.vec):1)
var.names<-label

plot(coef.vec, y.axis, type = "p", axes = F, xlab = "Log-Odds Estimates", ylab = "", pch = 20, cex = .65, xlim = c(-2,4), xaxs = "r", main = "Democratic", cex.main=.85, cex.lab=.75, mgp=c(2,.7,0)) 
#plot coefficients as points, turning off axes and labels. 

#set limits of x-axis so that they include mins and maxs of 
#coefficients + .95% confidence intervals and plot is symmetric; use "internal axes", and leave plot title empty
#the 3 lines below create horiztonal lines for 95% confidence intervals, and vertical ticks for 90% intervals

segments(coef.vec-qnorm(.975)*se.vec, y.axis, coef.vec+qnorm(.975)*se.vec, y.axis, lwd =  1, lend="butt")

#coef +/-1.96*se = 95% interval, lwd adjusts line thickness
#if you want to add tick marks for 90% confidence interval, use following 2 lines:
#segments(coef.vec-qnorm(.95)*se.vec, y.axis -.1, coef.vec-qnorm(.95)*se.vec, y.axis +.1, lwd = 1.1)#coef +/-1.64*se = 90% interval
#segments(coef.vec+qnorm(.95)*se.vec, y.axis -.1, coef.vec+qnorm(.95)*se.vec, y.axis +.1, lwd = 1.1)

axis(1, at = seq(-2,4,by=1), labels = NA, tick = T,#draw x-axis and labels with tick marks
    cex.axis = .65, mgp = c(2,.7,0))#reduce label size, moves labels closer to tick marks
axis(1, at = seq(-2,4,by=1), labels =  c(-2, -1, 0, 1,  2,  3, 4), tick = F,#draw x-axis and labels with tick marks
    cex.axis = .65, mgp = c(2,.7,0))#reduce label size, moves labels closer to tick marks    

axis(2, at = y.axis, label = var.names,   las = 1, tick = F, ,mgp = c(2,.6,0),
    cex.axis = .75) #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
segments(0,0,0,17,lty=2) # draw dotted line through 0
box(bty = "l") #place box around plot
x.height <- .20
#text(x.height, 1.75, expression(R^{2} == .13), adj = 0, cex = .75) #add text for R-squared
#text(x.height, 1.5, expression(beta[0] == 3.51~(.62)), adj = 0, cex = .75)#add text for Adjusted-R-squared
text(x.height, 1.25, "n = 629", adj = 0, cex = .75)#add text for sample size




rm(list=ls())
detach(d1)



#########ALL RESPONDENTS########



#par(mfrow=c(1,4),oma=c(0,0,2,0))
#par(mar=c(4, 7, 2, 2))

a1<-read.dta("allvoterm1.dta")
attach(a1)
coef.vec<-beta
se.vec<-se
y.axis<-c(length(coef.vec):1)
var.names<-label


plot(coef.vec, y.axis, type = "p", axes = F, xlab = "Log-Odds Estimates", ylab = "", pch = 20, cex = .65, xlim = c(-2,4), xaxs = "r", main = "All Respondents (incl. Ind.)", cex.main=.85, cex.lab=.75, mgp=c(2,.7,0)) 
#plot coefficients as points, turning off axes and labels. 

#set limits of x-axis so that they include mins and maxs of 
#coefficients + .95% confidence intervals and plot is symmetric; use "internal axes", and leave plot title empty
#the 3 lines below create horiztonal lines for 95% confidence intervals, and vertical ticks for 90% intervals

segments(coef.vec-qnorm(.975)*se.vec, y.axis, coef.vec+qnorm(.975)*se.vec, y.axis, lwd =  1, lend="butt")

#coef +/-1.96*se = 95% interval, lwd adjusts line thickness
#if you want to add tick marks for 90% confidence interval, use following 2 lines:
#segments(coef.vec-qnorm(.95)*se.vec, y.axis -.1, coef.vec-qnorm(.95)*se.vec, y.axis +.1, lwd = 1.1)#coef +/-1.64*se = 90% interval
#segments(coef.vec+qnorm(.95)*se.vec, y.axis -.1, coef.vec+qnorm(.95)*se.vec, y.axis +.1, lwd = 1.1)

axis(1, at = seq(-2,4,by=1), labels = NA, tick = T,#draw x-axis and labels with tick marks
    cex.axis = .65, mgp = c(2,.7,0))#reduce label size, moves labels closer to tick marks
axis(1, at = seq(-2,4,by=1), labels =  c(-2, -1,  0,  1, 2, 3, 4), tick = T,#draw x-axis and labels with tickmarks
    cex.axis = .65, mgp = c(2,.7,0))#reduce label size, moves labels closer to tick marks    

axis(2, at = y.axis, label = var.names,   las = 1, tick = F, ,mgp = c(2,.6,0),
    cex.axis = .75) #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
segments(0,0,0,19,lty=2) # draw dotted line through 0
box(bty = "l") #place box around plot
x.height <- .20
#text(x.height, 1.75, expression(R^{2} == .13), adj = 0, cex = .75) #add text for R-squared
#text(x.height, 1.5, expression(beta[0] == 3.51~(.62)), adj = 0, cex = .75)#add text for Adjusted-R-squared
text(x.height, 1.25, "n = 1569", adj = 0, cex = .75)#add text for sample size
title("Proportional Odds Models of Immigration Preferences (Uncondtional and Conditional Models)",  outer=TRUE)


rm(list=ls())
detach(a1)

#######INTERACTION MODELS##########

r1<-read.dta("repvoter.dta")
attach(r1)
coef.vec<-beta
se.vec<-se
y.axis<-c(length(coef.vec):1)
var.names<-label

plot(coef.vec, y.axis, type = "p", axes = F, xlab = "Log-Odds Estimates", ylab = "", pch = 20, cex = .65, xlim = c(-2,5), xaxs = "r",  cex.main=.85, cex.lab=.75, mgp=c(2,.7,0), main="Republican") 
#plot coefficients as points, turning off axes and labels. 

#set limits of x-axis so that they include mins and maxs of 
#coefficients + .95% confidence intervals and plot is symmetric; use "internal axes", and leave plot title empty
#the 3 lines below create horiztonal lines for 95% confidence intervals, and vertical ticks for 90% intervals

segments(coef.vec-qnorm(.975)*se.vec, y.axis, coef.vec+qnorm(.975)*se.vec, y.axis, lwd =  1)

#coef +/-1.96*se = 95% interval, lwd adjusts line thickness
#if you want to add tick marks for 90% confidence interval, use following 2 lines:
#segments(coef.vec-qnorm(.95)*se.vec, y.axis -.1, coef.vec-qnorm(.95)*se.vec, y.axis +.1, lwd = 1.1)#coef +/-1.64*se = 90% interval
#segments(coef.vec+qnorm(.95)*se.vec, y.axis -.1, coef.vec+qnorm(.95)*se.vec, y.axis +.1, lwd = 1.1)

axis(1, at = seq(-2,5,by=1), labels = NA, tick = T,#draw x-axis and labels with tick marks
    cex.axis = .65, mgp = c(2,.7,0))#reduce label size, moves labels closer to tick marks
axis(1, at = seq(-2,5,by=1), labels =  c(-2, -1, 0,  1, 2,  3, 4, 5), tick = T,#draw x-axis and labels with tick marks
    cex.axis = .65, mgp = c(2,.7,0))#reduce label size, moves labels closer to tick marks    

axis(2, at = y.axis, label = var.names,   las = 1, tick = F ,mgp = c(2,.6,0),
    cex.axis = .75) #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
segments(0,0,0,19,lty=2) # draw dotted line through 0
box(bty = "l") #place box around plot
x.height <- .20
#text(x.height, 1.75, expression(R^{2} == .13), adj = 0, cex = .75) #add text for R-squared
#text(x.height, 1.5, expression(beta[0] == 3.51~(.62)), adj = 0, cex = .75)#add text for Adjusted-R-squared
text(x.height, 1.25, "n = 771", adj = 0, cex = .75)#add text for sample size

rm(list=ls())
detach(r1)

####### Dem M1

rm(list=ls())


#par(mfrow=c(1,2),oma=c(0,0,2,0))
#par(mar=c(4, 7, 2, 2))

d1<-read.dta("demvoter.dta")
attach(d1)
coef.vec<-beta
se.vec<-se
y.axis<-c(length(coef.vec):1)
var.names<-label

plot(coef.vec, y.axis, type = "p", axes = F, xlab = "Log-Odds Estimates", ylab = "", pch = 20, cex = .65, xlim = c(-2,4), xaxs = "r", cex.main=.85, cex.lab=.75, mgp=c(2,.7,0), main="Democrat") 
#plot coefficients as points, turning off axes and labels. 

#set limits of x-axis so that they include mins and maxs of 
#coefficients + .95% confidence intervals and plot is symmetric; use "internal axes", and leave plot title empty
#the 3 lines below create horiztonal lines for 95% confidence intervals, and vertical ticks for 90% intervals

segments(coef.vec-qnorm(.975)*se.vec, y.axis, coef.vec+qnorm(.975)*se.vec, y.axis, lwd =  1)

#coef +/-1.96*se = 95% interval, lwd adjusts line thickness
#if you want to add tick marks for 90% confidence interval, use following 2 lines:
#segments(coef.vec-qnorm(.95)*se.vec, y.axis -.1, coef.vec-qnorm(.95)*se.vec, y.axis +.1, lwd = 1.1)#coef +/-1.64*se = 90% interval
#segments(coef.vec+qnorm(.95)*se.vec, y.axis -.1, coef.vec+qnorm(.95)*se.vec, y.axis +.1, lwd = 1.1)

axis(1, at = seq(-2,4,by=1), labels = NA, tick = T,#draw x-axis and labels with tick marks
    cex.axis = .65, mgp = c(2,.7,0))#reduce label size, moves labels closer to tick marks
axis(1, at = seq(-2,4,by=1), labels =  c(-2, -1, 0, 1,  2,  3, 4), tick = F,#draw x-axis and labels with tick marks
    cex.axis = .65, mgp = c(2,.7,0))#reduce label size, moves labels closer to tick marks    

axis(2, at = y.axis, label = var.names,   las = 1, tick = F, ,mgp = c(2,.6,0),
    cex.axis = .75) #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
segments(0,0,0,19,lty=2) # draw dotted line through 0
box(bty = "l") #place box around plot
x.height <- .20
#text(x.height, 1.75, expression(R^{2} == .13), adj = 0, cex = .75) #add text for R-squared
#text(x.height, 1.5, expression(beta[0] == 3.51~(.62)), adj = 0, cex = .75)#add text for Adjusted-R-squared
text(x.height, 1.25, "n = 629", adj = 0, cex = .75)#add text for sample size




rm(list=ls())
detach(d1)

#########ALL RESPONDENTS########



#par(mfrow=c(2,2),oma=c(0,0,2,0))
#par(mar=c(4, 7, 2, 2))

a1<-read.dta("allvoter.dta")
attach(a1)
coef.vec<-beta
se.vec<-se
y.axis<-c(length(coef.vec):1)
var.names<-label


plot(coef.vec, y.axis, type = "p", axes = F, xlab = "Log-Odds Estimates", ylab = "", pch = 20, cex = .65, xlim = c(-2,4), xaxs = "r", cex.main=.85, cex.lab=.75, mgp=c(2,.7,0), main="All Respondents (incl. Ind.)") 
#plot coefficients as points, turning off axes and labels. 

#set limits of x-axis so that they include mins and maxs of 
#coefficients + .95% confidence intervals and plot is symmetric; use "internal axes", and leave plot title empty
#the 3 lines below create horiztonal lines for 95% confidence intervals, and vertical ticks for 90% intervals

segments(coef.vec-qnorm(.975)*se.vec, y.axis, coef.vec+qnorm(.975)*se.vec, y.axis, lwd =  1)

#coef +/-1.96*se = 95% interval, lwd adjusts line thickness
#if you want to add tick marks for 90% confidence interval, use following 2 lines:
#segments(coef.vec-qnorm(.95)*se.vec, y.axis -.1, coef.vec-qnorm(.95)*se.vec, y.axis +.1, lwd = 1.1)#coef +/-1.64*se = 90% interval
#segments(coef.vec+qnorm(.95)*se.vec, y.axis -.1, coef.vec+qnorm(.95)*se.vec, y.axis +.1, lwd = 1.1)

axis(1, at = seq(-2,4,by=1), labels = NA, tick = T,#draw x-axis and labels with tick marks
    cex.axis = .65, mgp = c(2,.7,0))#reduce label size, moves labels closer to tick marks
axis(1, at = seq(-2,4,by=1), labels =  c(-2, -1,  0,  1, 2, 3, 4), tick = T,#draw x-axis and labels with tickmarks
    cex.axis = .65, mgp = c(2,.7,0))#reduce label size, moves labels closer to tick marks    

axis(2, at = y.axis, label = var.names,   las = 1, tick = F, ,mgp = c(2,.6,0),
    cex.axis = .75) #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
segments(0,0,0,20,lty=2) # draw dotted line through 0
box(bty = "l") #place box around plot
x.height <- .20
#text(x.height, 1.75, expression(R^{2} == .13), adj = 0, cex = .75) #add text for R-squared
#text(x.height, 1.5, expression(beta[0] == 3.51~(.62)), adj = 0, cex = .75)#add text for Adjusted-R-squared
text(x.height, 1.25, "n = 1569", adj = 0, cex = .75)#add text for sample size


dev.off()
